define(['common/js/filter/boss-filter-0.1','common/lib/emailtip/jquery.emailtip.js'],function(){

    diretiveModule.directive('mplusFormCheckbox', function($injector) {
        return {
            scope : {
                //name: '@',
                //label: '@',
                list : '=',
                //listKey: '@',
                //listLabel: '@',
                //listTitle: '@',
            },
            restrict : "AE",
            //replace : true,
            controller : function($scope){
                // 校验
                if(!angular.isArray($scope.list)){
                    console.error("list属性值必须是对应的scope中的数组");
                    return;
                }
            },
            template: function(elm,attrs){
                if(!attrs.list) {
                    console.error("必须的属性有list listKey listLabel name");
                    return;
                }

                var template = '<div class="form-group">';
                if(attrs.label){
                    template +=	'<label class="col-sm-3 control-label">'+attrs.label+'</label>';

                }
                template +=	('<div class="col-sm-9">'+
                '<div class="checkbox">'+
                '<label ng-repeat="option in list"><input name="'+attrs.name+'" type="checkbox" ');
                if(attrs.listTitle){
                    template += 'title="{{option.'+attrs.listTitle+'}}"  ';
                }
                template += ('ng-model="option.checked">{{option.'+attrs.listLabel+'}}&nbsp;&nbsp;&nbsp;</label></div></div></div>');

                return template;
            }
        };
    });

    diretiveModule.directive('mplusFormDownCheckbox', function($injector) {
        return {
            scope : {
                //name: '@',
                //label: '@',
                list : '=',
                //listKey: '@',
                //listLabel: '@',
                //listTitle: '@',
            },
            restrict : "AE",
            //replace : true,
            controller : function($scope,$element,$attrs,$transclude){
                // 校验
                if(!angular.isArray($scope.list)){
                    console.error("list属性值必须是对应的scope中的数组");
                    return;
                }

                //
                $scope.checkOption = function(option){
                    option.checked = !option.checked;
                    var children = option[$attrs.listChildren];
                    if(children && children.length > 0){
                        for(var i in children){
                            children[i].checked = option.checked;
                        }
                    }
                };
                $scope.checkChild = function(child,option){
                    child.checked = !child.checked;
                    var i,flag,
                        children = option[$attrs.listChildren],
                        initFlag = !!children[0].checked;
                    for(i in children){
                        if(!!children[i].checked != initFlag){
                            option.checked = false;
                            return;
                        }
                    }

                    if(initFlag){
                        option.checked = true;
                    }
                };
            },
            template: function(elm,attrs){
                if(!attrs.list) {
                    console.error("必须的属性有list listKey listLabel listChildren name");
                    return;
                }

                var childrenStr = 'option.'+attrs.listChildren,
                    childrenShow = childrenStr+' && '+childrenStr+'.length > 0';

                var template = '';
                if(attrs.label){
                    template += attrs.label;
                }
                template += '<ul class="nav navbar-nav">';
                template += '    <li class="dropdown open" ng-repeat="option in list">';
                template += '        <div class="checkbox">';
                template += '            <label> <input type="checkbox" ng-click="checkOption(option)" checked="checked" ng-model="option.checked" name="'+attrs.name+'">{{option.'+attrs.listLabel+'}}</label>';
                template += '            <span ng-show="option.checked">（全部）</span><span ng-show="!option.checked">（部分）</span>';
                template += '            <span ng-show="'+childrenShow+'" class="caret"></span>';
                template += '        </div>';
                template += '        <ul ng-show="'+childrenShow+'" class="dropdown-menu">';
                template += '            <li ng-repeat="child in '+childrenStr+'"> <div class="checkbox"><label> <input type="checkbox" ng-click="checkChild(child,option)" ng-model="child.checked"  name="child.'+attrs.name+'"> {{child.'+attrs.listLabel+'}} </label></div> </li>';
                template += '        </ul>';
                template += '    </li>';
                template += '</ul>';

                return template;
            }
        };
    });

    diretiveModule.directive('mplusFormRadio', function($injector) {
        return {
            scope : {
                //name: '@',
                //label: '@',
                list : '=',
                //listKey: '@',
                //listLabel: '@',
                //listTitle: '@',
            },
            restrict : "AE",
            replace : true,
            controller : function($scope){
                // 校验
                if(!angular.isArray($scope.list)){
                    console.error("list属性值必须是对应的scope中的数组");
                    return;
                }
            },
            template: function(elm,attrs){
                if(!attrs.list) {
                    console.error("必须的属性有list listKey listLabel name");
                    return;
                }

                var template = '<div class="form-group">';
                if(attrs.label){
                    template +=	'<label class="col-sm-3 control-label">'+attrs.label+'</label>';

                }
                template +=	('<div class="col-sm-9">'+
                '<div class="radio">'+
                '<label ng-repeat="option in list"><input name="'+attrs.name+'" type="radio" ');
                if(attrs.listTitle){
                    template += 'title="{{option.'+attrs.listTitle+'}}"  ';
                }
                template += ('ng-model="option.checked">{{option.'+attrs.listLabel+'}}&nbsp;&nbsp;&nbsp;</label></div></div></div>');

                return template;
            }
        };
    });

});